package com.tjtt.tdm.form.dao.impl;

import java.util.List;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.Query;

import com.tjtt.tdm.base.BaseHibernateDao;
import com.tjtt.tdm.base.PageResult;
import com.tjtt.tdm.flow.bean.FlowDetailed;
import com.tjtt.tdm.flow.bean.FlowTask;
import com.tjtt.tdm.flow.bean.FlowTaskInfo;
import com.tjtt.tdm.flow.bean.TdmCustomNo;
import com.tjtt.tdm.form.bean.AutoFormsButton;
import com.tjtt.tdm.form.bean.AutoFormsField;
import com.tjtt.tdm.form.bean.AutoFormsNew;
import com.tjtt.tdm.form.dao.FlowtypeDao;
import com.tjtt.tdm.form.dao.IAutoFormDao;

public class AutoFormDaoImpl extends BaseHibernateDao implements IAutoFormDao{
	private static final Log log = LogFactory.getLog(FlowtypeDao.class);
	
	public int insertCreateSequencesSql(String sequencesName) {
		String createSequencesSql = "create sequence seq_"+sequencesName+" minvalue 1 maxvalue 999999999999999999999 start with 1 increment by 1 cache 20";
		Query query = getSession().createSQLQuery(createSequencesSql);
		query.executeUpdate();
		return 0;
	}

	
	public int insertCreateTableSql(String tableName) {
		String createTableSql = "create table "+tableName+"( id number not null )";
		String createTablePk = "alter table "+tableName+" add constraint PK_"+tableName+" primary key (ID)"+
		" using index tablespace TDM"+
		" pctfree 10 initrans 2 maxtrans 255 storage ( initial 64K minextents 1  maxextents unlimited )";
		
		Query query = getSession().createSQLQuery(createTableSql);
		query.executeUpdate();
		
		Query query1 = getSession().createSQLQuery(createTablePk);
		query1.executeUpdate();
		return 0;
	}
	
	
	public String saveAutoFormsNew(AutoFormsNew autoFormNew) {
		return this.getHibernateTemplate().save(autoFormNew).toString();
	}
	
	
	public PageResult queryAutoFormsListNewData(Map filterMap) {
		StringBuffer queryString = new StringBuffer();
		StringBuffer queryCountString = new StringBuffer();
		
		queryCountString.append(" select count(*) from auto_forms_new where tabletype=0 ");
		queryString.append("SELECT t.ID,t.form_cn_name,form_en_name,t.SUOSLX,t.form_path,t.form_note,t.form_column,t.form_format,t.create_name,t.create_time,t.is_delete_table FROM auto_forms_new t where tabletype=0 ");
		if (filterMap!= null && !filterMap.isEmpty()) {
			if(null != filterMap.get("form_cn_name") &&! "".equals(filterMap.get("form_cn_name"))){
				queryString.append("and form_cn_name like '%" + filterMap.get("form_cn_name") + "%'  ");
				queryCountString.append("and form_cn_name like '%" + filterMap.get("form_cn_name") + "%'  ");
			}
			if(null != filterMap.get("form_en_name") &&! "".equals(filterMap.get("form_en_name"))){
				queryString.append("and form_en_name like upper('%" + filterMap.get("form_en_name") + "%')  ");
				queryCountString.append("and form_en_name like upper('%" + filterMap.get("form_en_name") + "%')  ");
			}
			if(null != filterMap.get("type") &&! "".equals(filterMap.get("type"))){
				queryString.append("and form_type = '" + filterMap.get("type") + "'  ");
				queryCountString.append("and form_type = '" + filterMap.get("type") + "'  ");
			}
			String sidx = (String) filterMap.get("sidx");
			String sord = (String) filterMap.get("sord");
			queryString.append("order by " + sidx + " " + sord + " ");
		}
		return searchBySQL(queryCountString.toString(), queryString.toString(), filterMap);
	}
	public PageResult queryAutoFormsListNew(Map filterMap) {
		StringBuffer queryString = new StringBuffer();
		StringBuffer queryCountString = new StringBuffer();
		
		queryCountString.append(" select count(*) from auto_forms_new where 1=1 ");
		queryString.append("SELECT t.ID,t.form_cn_name,form_en_name,t.SUOSLX,t.form_path,t.form_note,t.form_column,t.form_format,t.create_name,t.create_time,t.is_delete_table FROM auto_forms_new t where 1=1 ");
		if (filterMap!= null && !filterMap.isEmpty()) {
			if(null != filterMap.get("form_cn_name") &&! "".equals(filterMap.get("form_cn_name"))){
				queryString.append("and form_cn_name like '%" + filterMap.get("form_cn_name") + "%'  ");
				queryCountString.append("and form_cn_name like '%" + filterMap.get("form_cn_name") + "%'  ");
			}
			if(null != filterMap.get("form_en_name") &&! "".equals(filterMap.get("form_en_name"))){
				queryString.append("and form_en_name like upper('%" + filterMap.get("form_en_name") + "%')  ");
				queryCountString.append("and form_en_name like upper('%" + filterMap.get("form_en_name") + "%')  ");
			}
			if(null != filterMap.get("type") &&! "".equals(filterMap.get("type"))){
				queryString.append("and form_type = '" + filterMap.get("type") + "'  ");
				queryCountString.append("and form_type = '" + filterMap.get("type") + "'  ");
			}
			String sidx = (String) filterMap.get("sidx");
			String sord = (String) filterMap.get("sord");
			queryString.append("order by " + sidx + " " + sord + " ");
		}
		return searchBySQL(queryCountString.toString(), queryString.toString(), filterMap);
	}
	public PageResult queryAutoFormsFieldListDicByFormId(Map filterMap) {
		StringBuffer queryString = new StringBuffer();
		StringBuffer queryCountString = new StringBuffer();
		
		queryCountString.append(" select count(*) from auto_forms_field t where 1=1 ");
		queryString.append(" select t.id,t.cn_name,t.field_name,t.field_note,t.dictionarytable,t.field_type,"+
					      " t.is_read_only,t.authentication_type,t.is_list_show,"+
					      " t.is_page_show,t.show_seq,t.is_column_merge,t.field_list_width,t.is_order_by,t.default_value"+
					      " from auto_forms_field t where 1 = 1 ");
		if (filterMap!= null && !filterMap.isEmpty()) {
			if(null != filterMap.get("forms_id") &&! "".equals(filterMap.get("forms_id"))){
				queryString.append(" and FORMS_ID = '" + filterMap.get("forms_id") + "'  ");
				queryCountString.append(" and FORMS_ID = '" + filterMap.get("forms_id") + "'  ");
//				queryString.append(" and searchtype = " + filterMap.get("searchType") + "  ");
//				queryCountString.append(" and searchtype = " + filterMap.get("searchType")+ "  ");
			}
			if(null != filterMap.get("searchtype") &&! "".equals(filterMap.get("searchtype"))){
				queryString.append(" and searchtype = " + filterMap.get("searchType") + "  ");
				queryCountString.append(" and searchtype = " + filterMap.get("searchType")+ "  ");
			}
			String sidx = (String) filterMap.get("sidx");
			String sord = (String) filterMap.get("sord");
			queryString.append("order by " + sidx + " " + sord + " ");
		}
		return searchBySQL(queryCountString.toString(), queryString.toString(), filterMap);
	}
	
	
	public PageResult queryAutoFormsFieldListByFormId(Map filterMap) {
		StringBuffer queryString = new StringBuffer();
		StringBuffer queryCountString = new StringBuffer();
		
		queryCountString.append(" select count(*) from auto_forms_field t where 1=1 ");
		queryString.append(" select t.id,t.cn_name,t.field_name,t.field_note,t.field_type,"+
					      " t.is_read_only,t.authentication_type,t.is_list_show,"+
					      " t.is_page_show,t.show_seq,t.is_column_merge,t.field_list_width,t.is_order_by,t.default_value"+
					      " from auto_forms_field t where 1 = 1 ");
		if (filterMap!= null && !filterMap.isEmpty()) {
			if(null != filterMap.get("forms_id") &&! "".equals(filterMap.get("forms_id"))){
				queryString.append(" and FORMS_ID = '" + filterMap.get("forms_id") + "'  ");
				queryCountString.append(" and FORMS_ID = '" + filterMap.get("forms_id") + "'  ");
			}
			String sidx = (String) filterMap.get("sidx");
			String sord = (String) filterMap.get("sord");
			queryString.append("order by " + sidx + " " + sord + " ");
		}
		return searchBySQL(queryCountString.toString(), queryString.toString(), filterMap);
	}
	
	
	public void saveAutoFormsField(AutoFormsField autoFormsField) {
		this.getHibernateTemplate().save(autoFormsField);
	}
	
	
	public List queryAutoFormListByFormEnName(String formEnName) {
		String sql = "select * from AUTO_FORMS_NEW t where t.FORM_EN_NAME = upper('"+formEnName+"')";
		Query query = getSession().createSQLQuery(sql);
		List list = query.list();
		return list;
	}

	
	public void deleteTableAndSeqByFormName(String tableName) {
		String dropTableSql = "drop table "+tableName;
		String dropSequenceSql = "drop sequence seq_"+tableName;
		
		Query query = getSession().createSQLQuery(dropTableSql);
		query.executeUpdate();
		
		Query query1 = getSession().createSQLQuery(dropSequenceSql);
		query1.executeUpdate();
	}
	
	
	public void deleteAutoFormById(String id) {
		AutoFormsNew autoFormNew = new AutoFormsNew();
		autoFormNew.setId(Long.parseLong(id));
		this.getHibernateTemplate().delete(autoFormNew);
	}
	
	
	public void deleteAutoFormFieldByFormId(String formId) {
		String sql = "delete from AUTO_FORMS_FIELD t where t.FORMS_ID = '"+formId+"'";
		Query query = getSession(). createSQLQuery(sql);
		query.executeUpdate();
	}
	
	
	public List getTableColumnAndCommentAndDataType(String tableName) {
		String sql = "select distinct t.table_name, t.column_name, t.comments, t1.DATA_TYPE,t1.DATA_LENGTH"+
					 " from USER_COL_COMMENTS t "+
					 " left join user_tab_columns t1 on t.column_name = t1.COLUMN_NAME "+
					 " and t.table_name = t1.TABLE_NAME "+
					 " where t.TABLE_NAME = upper('"+tableName+"')";
		Query query = getSession().createSQLQuery(sql);
		List list = query.list();
		return list;
	}
	
	
	public AutoFormsNew getAutoFormsNewById(String formId) {
		AutoFormsNew af = (AutoFormsNew)this.getHibernateTemplate().get(AutoFormsNew.class, Long.parseLong(formId));
		return af;
	}

	
	public List<AutoFormsField> getAutoFormsFieldByformid(String formid) {
		String hql = "from AutoFormsField a where a.formsId="+formid +" order by  SHOW_SEQ,id asc";
		List<AutoFormsField> list = getHibernateTemplate().find(hql);
		return list;
	}
	
	
	public List<AutoFormsField> getAutoFormsFieldValues(String formid,String formName,String taskid) {
		String sql = "select  wmsys.wm_concat(field_name) from (select t.field_name from auto_forms_field t  where t.forms_id="+formid+" order by SHOW_SEQ,id asc)";
		Query query = this.getSession().createSQLQuery(sql);
		List list = query.list();
		String columns = list.get(0).toString();
		sql = "select " + columns + " from " + formName + " where ID=" + taskid;
		query = this.getSession().createSQLQuery(sql);
		return query.list();
	}
	
	
	public void addFormFieldByTable(String tableName,AutoFormsField autoFormsField) {
		String sqlFieldSql = "";
		if("VARCHAR2".equals(autoFormsField.getFieldType())){
			sqlFieldSql = "alter table "+tableName+" add "+autoFormsField.getFieldName()+" varchar2("+autoFormsField.getFieldLength()+")";
		}
		else if("DATE".equals(autoFormsField.getFieldType()))
		{
			sqlFieldSql = "alter table "+tableName+" add "+autoFormsField.getFieldName()+" date";
		}
		else 
		{
			sqlFieldSql = "alter table "+tableName+" add "+autoFormsField.getFieldName()+" number";
		}
		
		String commentSql = "comment on column "+tableName+"."+autoFormsField.getFieldName()+" is '"+autoFormsField.getCnName()+"'";

		Query query = getSession().createSQLQuery(sqlFieldSql);
		query.executeUpdate();
		
		Query query1 = getSession().createSQLQuery(commentSql);
		query1.executeUpdate();
	}
	
	
	public AutoFormsField getAutoFormFieldById(String id) {
		AutoFormsField aff = (AutoFormsField)this.getHibernateTemplate().get(AutoFormsField.class, Long.parseLong(id));
		return aff;
	}
	
	
	public void updateAutoFormField(AutoFormsField autoFormsField) {
		this.getHibernateTemplate().update(autoFormsField);
	}
	
	
	public void deleteAutoFormFieldById(String id) {
		AutoFormsField aff =  new AutoFormsField();
		aff.setId(Long.parseLong(id));
		this.getHibernateTemplate().delete(aff);
	}
	
	
	public void deleteFormFieldByTableAndColumn(String tableName,
			String columnName) {
		String deleteSql = "alter table " +tableName+" drop column " + columnName;
		Query query = getSession().createSQLQuery(deleteSql);
		query.executeUpdate();
	}
	
	
	public void updateAutoForm(AutoFormsNew autoFormNew) {
		this.getHibernateTemplate().update(autoFormNew);
	}
	
	public List getDatabySql(String sql) {
		Query query = getSession().createSQLQuery(sql);
		return query.list(); 
	}
	
	public String executeSql(String sql) {
		Query query = getSession().createSQLQuery(sql);
		int count = query.executeUpdate();
		if(count>0){
		}
		return null;
	}
	
	
	public PageResult queryAutoFormsButtonList(Map filterMap) {
		StringBuffer queryString = new StringBuffer();
		StringBuffer queryCountString = new StringBuffer();
		
		queryCountString.append(" select count(*) from auto_forms_button t "+
								" left join auto_forms_new t1 on t.this_table_name_id = t1.id "+
								" left join auto_forms_new t2 on t.by_table_name_id = t2.id "+
								" where 1=1 ");
		queryString.append("select t.id,t.button_name,t.this_table_name_id,t1.form_cn_name as this_table_name,"+
							" t.this_table_column,t.by_table_name_id,t2.form_en_name as by_table_name,t.by_table_column,t.button_type,t.operate_type from auto_forms_button t "+
							" left join auto_forms_new t1 on t.this_table_name_id = t1.id "+
							" left join auto_forms_new t2 on t.by_table_name_id = t2.id "+
							" where 1=1 ");
		if (filterMap!= null && !filterMap.isEmpty()) {
			if(null != filterMap.get("this_table_name_id") && !"".equals(filterMap.get("this_table_name_id")) & !"null".equals(filterMap.get("this_table_name_id"))){
				queryString.append(" and t.this_table_name_id = " + filterMap.get("this_table_name_id"));
				queryCountString.append(" and t.this_table_name_id = " + filterMap.get("this_table_name_id"));
			}
			String sidx = (String) filterMap.get("sidx");
			String sord = (String) filterMap.get("sord");
			queryString.append(" order by " + sidx + " " + sord + " ");
		}
		return searchBySQL(queryCountString.toString(), queryString.toString(), filterMap);
	}
	
	
	public void saveAutoFormsButton(AutoFormsButton autoFormsButton) {
		this.getHibernateTemplate().save(autoFormsButton);
	}
	
	
	public AutoFormsButton getAutoFormButtonById(String id) {
		AutoFormsButton formButton = (AutoFormsButton)this.getHibernateTemplate().get(AutoFormsButton.class, Long.parseLong(id));
		return formButton;
	}
	
	
	public void deleteAutoFormsButtonById(String ids) {
		AutoFormsButton afb = new AutoFormsButton();
		afb.setId(Long.parseLong(ids));
		this.getHibernateTemplate().delete(afb);
	}

	
	public void deleteFlowTaskById(String ids) {
		FlowTask flowTask = new FlowTask();
		flowTask.setId(Long.parseLong(ids));
		this.getHibernateTemplate().delete(flowTask);
	}

	
	public void deleteFlowTaskInFoByFlowTaskId(String ids) {
		String sql = "delete from FLOW_TASK_INFO t where t.flowtaskid = '"+ids+"'";
		Query query = getSession(). createSQLQuery(sql);
		query.executeUpdate();
	}

	
	public PageResult queryAutoFormsField(Map<String, String> filterMap) {
		StringBuffer queryString = new StringBuffer();
		StringBuffer queryCountString = new StringBuffer();
		
		queryCountString.append(" select count(*) from AUTO_FORMS_FIELD t where 1=1 ");
		queryString.append("SELECT t.ID,t.CN_NAME ,t.FIELD_NAME FROM AUTO_FORMS_FIELD t where 1=1 ");
		if (filterMap!= null && !filterMap.isEmpty()) {
			if(null != filterMap.get("field_cn_name") &&! "".equals(filterMap.get("field_cn_name"))){
				queryString.append("and t.CN_NAME like '%" + filterMap.get("field_cn_name") + "%'  ");
				queryCountString.append("and t.CN_NAME like '%" + filterMap.get("field_cn_name") + "%'  ");
			}
			if(null != filterMap.get("field_en_name") &&! "".equals(filterMap.get("field_en_name"))){
				queryString.append("and t.FIELD_NAME like upper('%" + filterMap.get("field_en_name") + "%')  ");
				queryCountString.append("and t.FIELD_NAME like upper('%" + filterMap.get("field_en_name") + "%')  ");
			}
			if(null != filterMap.get("formsId") &&! "".equals(filterMap.get("formsId"))){
				queryString.append("and t.FORMS_ID = upper('" + filterMap.get("formsId") + "')  ");
				queryCountString.append("and t.FORMS_ID = upper('" + filterMap.get("formsId") + "')  ");
			}
			String sidx = (String) filterMap.get("sidx");
			String sord = (String) filterMap.get("sord");
			queryString.append("order by " + sidx + " " + sord + " ");
		}
		return searchBySQL(queryCountString.toString(), queryString.toString(), filterMap);
	}

	
	public void removeView(String ids) {
		String sql = "delete from FLOW_TASK t where t.id = '"+ids+"'";
		Query query = getSession(). createSQLQuery(sql);
		query.executeUpdate();
	}
	public FlowTask getFlowTaskById(Long ids) {
		FlowTask flowTask  = (FlowTask) super.getHibernateTemplate().get(FlowTask.class ,ids);
		return flowTask;
	}

	
	public void deleteFormTable(Long mainId, String formEnName) {
		String sql = "delete from "+formEnName+" t where t.id = '"+mainId+"'";
		Query query = getSession(). createSQLQuery(sql);
		query.executeUpdate();
	}

	
	public void updateAutoFormButton(AutoFormsButton afb) {
		this.getHibernateTemplate().update(afb);
	}

	public PageResult queryProjectTask(Map filterMap) {
		StringBuffer queryString = new StringBuffer();
		StringBuffer queryCountString = new StringBuffer();
		
		queryCountString.append(" select count(*) from project_task t where 1=1 ");
		queryString.append("select t.id,t.sid,t.task_name,t.assignments from project_task t where 1=1 ");
		if (filterMap!= null && !filterMap.isEmpty()) {
			if(null != filterMap.get("taskName") &&! "".equals(filterMap.get("taskName"))){
				queryString.append("and t.task_name like '%" + filterMap.get("taskName") + "%'  ");
				queryCountString.append("and t.task_name like '%" + filterMap.get("taskName") + "%'  ");
			}
			if(null != filterMap.get("assignments") &&! "".equals(filterMap.get("assignments"))){
				queryString.append("and t.assignments = upper('" + filterMap.get("assignments") + "')  ");
				queryCountString.append("and t.assignments = upper('" + filterMap.get("assignments") + "')  ");
			}
			if(null != filterMap.get("projectid") &&! "".equals(filterMap.get("projectid"))){
				queryString.append("and t.project_id = " + Long.parseLong(filterMap.get("projectid").toString()) );
				queryCountString.append("and t.project_id = "  + Long.parseLong(filterMap.get("projectid").toString()) );
			}
			if(null != filterMap.get("taskid") &&! "".equals(filterMap.get("taskid"))){
				queryString.append("and t.task_id <> " + Long.parseLong(filterMap.get("taskid").toString()) );
				queryCountString.append("and t.task_id <> "  + Long.parseLong(filterMap.get("taskid").toString()) );
			}
			String sidx = (String) filterMap.get("sidx");
			String sord = (String) filterMap.get("sord");
			queryString.append("order by " + sidx + " " + sord + " ");
		}
		return searchBySQL(queryCountString.toString(), queryString.toString(), filterMap);
	}

	/**
	 * 查询此表中是否存在此字段名称
	 * @author zhn
	 * @date Feb 17, 2014 2:27:16 PM
	 * @param fieldName
	 * @return
	 */
	public String findFieldName(String fieldName,String formId) {
		String sql = "select count(1) from AUTO_FORMS_FIELD t where t.field_name='"+fieldName+"'  and t.forms_id = "+formId+"";
		Query query = this.getSession().createSQLQuery(sql);
		List list = query.list();
		getSession().clear();
		return String.valueOf(list.get(0));
	}
	public void insertDetailTable(String tableEnName) {
		//复制表
		String insertTableSql = "create table "+tableEnName+"_b as select * from "+tableEnName+" where 1=2 ";
		Query query = getSession().createSQLQuery(insertTableSql);
		query.executeUpdate();
		
		//在复制的表中添加一个flow_state 字段
		String addFlowStateSql = "alter table "+tableEnName+"_b add (flow_state VARCHAR2(1000),flow_main_id number) ";
		Query query1 = getSession().createSQLQuery(addFlowStateSql);
		query1.executeUpdate();
		
		//创建序列
		String createSequencesSql = "create sequence seq_"+tableEnName+"_b minvalue 1 maxvalue 999999999999999999999 start with 1 increment by 1 cache 20";
		Query query2 = getSession().createSQLQuery(createSequencesSql);
		query2.executeUpdate();
		
	}

	
	public void insertTable(String tableName,String formEnName) {
		//复制表
		String insertTableSql = "create table "+tableName+" as select * from "+formEnName+" where 1=2 ";
		Query query = getSession().createSQLQuery(insertTableSql);
		query.executeUpdate();
		
		//在复制的表中添加一个flow_state 字段
		//String addFlowStateSql = "alter table "+tableName+" add (flow_state VARCHAR2(1000),flow_main_id number) ";
		//Query query1 = getSession().createSQLQuery(addFlowStateSql);
		//query1.executeUpdate();
		
		//创建序列
		String createSequencesSql = "create sequence seq_"+tableName+" minvalue 1 maxvalue 999999999999999999999 start with 1 increment by 1 cache 20";
		Query query2 = getSession().createSQLQuery(createSequencesSql);
		query2.executeUpdate();
	}
	
	public String queryNo(String auToFormNewId) {
		Query query = this.getSession().createSQLQuery(auToFormNewId);
		List list = query.list();
		getSession().clear();
		return String.valueOf(list.get(0));
	}

	
	public String saveFlowTask(FlowTask flowTask) {
		return this.getHibernateTemplate().save(flowTask).toString();
	}

	
	public void updateFlowTask(FlowTask flowTask) {
		this.getHibernateTemplate().update(flowTask);
	}

	
	public void executeSql1(String querySql0) {
		getSession().beginTransaction().begin();
		Query query = getSession().createSQLQuery(querySql0);
		query.executeUpdate();
		getSession().beginTransaction().commit();
		 
	}

	
	public void saveFlowDetailed(FlowDetailed flowDetailed) {
		this.getHibernateTemplate().save(flowDetailed);
	}
	
	
	public void saveCustomNo(TdmCustomNo tcn) {
		this.getHibernateTemplate().save(tcn);
	}
	
	
	public PageResult queryCustomNoList(Map<String, String> filterMap) {
		StringBuffer queryString = new StringBuffer();
		StringBuffer queryCountString = new StringBuffer();
		
		queryCountString.append(" select count(*) from tdm_custom_no t where 1=1 ");
		queryString.append("SELECT t.id,t.custom_name,t.custom_num ,t.cur_number FROM tdm_custom_no t where 1=1 ");
		if (filterMap!= null && !filterMap.isEmpty()) {
			/*if(null != filterMap.get("field_cn_name") &&! "".equals(filterMap.get("field_cn_name"))){
				queryString.append("and t.CN_NAME like '%" + filterMap.get("field_cn_name") + "%'  ");
				queryCountString.append("and t.CN_NAME like '%" + filterMap.get("field_cn_name") + "%'  ");
			}*/
			String sidx = (String) filterMap.get("sidx");
			String sord = (String) filterMap.get("sord");
			queryString.append("order by " + sidx + " " + sord + " ");
		}
		return searchBySQL(queryCountString.toString(), queryString.toString(), filterMap);
	}
	
	
	public TdmCustomNo queryTdmCustomNoById(Long id) {
		TdmCustomNo tdmCustomNo  = (TdmCustomNo) super.getHibernateTemplate().get(TdmCustomNo.class ,id);
		return tdmCustomNo;
	}
	
	
	public void updateCustom(TdmCustomNo tcn) {
		this.getHibernateTemplate().update(tcn);
	}

	
	public FlowTaskInfo getFlowTaskInfoByTaskId(String flowtaskid) {
		String sql = "from FlowTaskInfo u where u.flowtaskid = "+flowtaskid+" ";
		Query sqlQuery = this.getSession().createQuery(sql);
		List list = sqlQuery.list();
		if(list!=null&&list.size()>0){
			FlowTaskInfo lhTest = (FlowTaskInfo) list.get(0);
			return lhTest;
		}else{
			return null;
		}
	}

	
	public void updateFlowTaskInfo(FlowTaskInfo flowTaskInfo) {
		this.getHibernateTemplate().update(flowTaskInfo);
	}

	
	public List findAllBySql(String typeSql) {
		log.debug("finding all Flowtype instances");
		try {
			Query query = getSession().createSQLQuery(typeSql);
			List list = query.list();
			return list;
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}

	
	public String queryTdmItem(String taskId) {
		String sql = " select count(*)  from TDM_ITEM t where t.flow_task_id = "+taskId+" ";
		Query query = getSession().createSQLQuery(sql);
		List list = query.list();
		return list.get(0).toString();
	}

	
	public void updateCustomNo(TdmCustomNo tcn) {
		this.getHibernateTemplate().update(tcn);
	}

	
	public TdmCustomNo queryTdmCustomNo(long id) {
		TdmCustomNo af = (TdmCustomNo)this.getHibernateTemplate().get(TdmCustomNo.class,id);
		return af;
	}
	
	public void savePriority(String ids, String formid) {
		String[] idss = ids.split(",");
		for (int i = 0; i < idss.length; i++) {
			String id = idss[i];
			//String sql = "update tdm_spare_part t set t.show_seq="+String.valueOf(i)+" where t.id="+id;
			String sql = "update auto_forms_field t set t.show_seq="+String.valueOf(i)+" where t.id="+id;
			getSession().createSQLQuery(sql).executeUpdate();
		}
	}

	public List queryBySql(String sql) {
		Query query = getSession().createSQLQuery(sql);
		return query.list();
	}
	
	public void executeBySql(String sql){
		Query query = getSession().createSQLQuery(sql);
		query.executeUpdate();
	}

}
